package com.zmt.mrms.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zmt.mrms.entity.UserAdmin;
import com.zmt.mrms.entity.UserDoctor;
import com.zmt.mrms.service.AdminService;

import jakarta.annotation.Resource;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class AdminController {

	@Resource
	private AdminService adminService;

	private HttpServletRequest request;
	private HttpServletResponse response;

	protected AdminController(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}

	protected void setCookie(String id, String role) {
		Cookie cookie1 = new Cookie("id", id);
		Cookie cookie2 = new Cookie("role", role);
		cookie1.setHttpOnly(true);
		cookie2.setHttpOnly(true);
		cookie1.setMaxAge(-1);
		cookie2.setMaxAge(-1);
		cookie1.setPath("/");
		cookie2.setPath("/");
		this.response.addCookie(cookie1);
		this.response.addCookie(cookie2);
	}

	protected void removeCookie() {
		Cookie cookie1 = new Cookie("id", null);
		Cookie cookie2 = new Cookie("role", null);
		Cookie cookie3 = new Cookie("JSESSIONID", null);
		cookie1.setMaxAge(0);
		cookie2.setMaxAge(0);
		cookie3.setMaxAge(0);
		cookie1.setPath("/");
		cookie2.setPath("/");
		cookie3.setPath("/");
		this.response.addCookie(cookie1);
		this.response.addCookie(cookie2);
		this.response.addCookie(cookie3);
	}

	@GetMapping("/adminManage")
	public String adminManage(Model model) {
//		model.addAttribute();
		model.addAttribute("page", "adminManage");
		return "homepageFA";
	}

	@GetMapping("/medRecManage")
	public String medRecManage(Model model) {
		model.addAttribute("fatherList", adminService.redicalRecordManagement());
		model.addAttribute("page", "medRecManage");
		return "homepageFA";
	}

	@GetMapping("/patientManage")
	public String patientManage(Model model, String fuzzyQuery) {
		model.addAttribute("patientList", adminService.getPatient(fuzzyQuery));
		model.addAttribute("page", "patientManage");
		return "homepageFA";
	}

	@GetMapping("/patientManage/blockOpreation")
	public String patientBlock(String id, Boolean status) {
//		System.out.println(id + " " + status);
		adminService.changePatientStatus(Integer.parseInt(id), status);
		return "redirect:/admin/patientManage";
	}

	@GetMapping("/doctorManage")
	public String doctorManage(Model model, String section, String position, String fuzzyQuery) {
		model.addAttribute("filter", adminService.getFilterList());
		model.addAttribute("doctorList", adminService.getLimitedDoctor(section, position, fuzzyQuery));
		model.addAttribute("page", "doctorManage");
		return "homepageFA";
	}

	@GetMapping("/doctorManage/blockOpreation")
	public String doctorBlock(String id, Boolean status) {
//		System.out.println(id + " " + status);
		adminService.changeDoctorStatus(Integer.parseInt(id), status);
		return "redirect:/admin/doctorManage";
	}

	@GetMapping("/doctorManage/addDoctor")
	public String addDoctorPage(Model model, String id) {
		if (id != null) {
			model.addAttribute("user", adminService.findOneDoctor(Integer.parseInt(id)));
		}
		return "admin/addDoctor";
	}

	@PostMapping("/doctorManage/addDoctor")
	public String addDoctor(Model model, UserDoctor userDoctor, int type) {
		if (type == 1) {
			System.out.println(userDoctor);
			model.addAttribute("user", adminService.addDoctor(userDoctor));
			model.addAttribute("success", "新增成功");
		}
		if (type == 2) {
			model.addAttribute("user", adminService.updateDoctor(userDoctor));
			model.addAttribute("success", "更新成功");
		}
		return "admin/addDoctor";
	}

	@GetMapping("/accountInfo/reset")
	public String resetAccount(Model model, @CookieValue(value = "id", required = false) String id) {
		adminService.resetAdminAccount(Integer.parseInt(id));
		removeCookie();
		return "redirect:/admin";
	}

	@PostMapping("/accountInfo/update")
	public String changeAccountInfo(String account, String password, Model model,
			@CookieValue(value = "id", required = false) String id) {
		adminService.updateAdminInfo(account, password, Integer.parseInt(id));
		return "redirect:/admin/accountInfo";
	}

	@GetMapping("/accountInfo")
	public String accountInfo(Model model, @CookieValue(value = "id", required = false) String id,
			@RequestParam(value = "update", required = false) String update) {
		UserAdmin userAdmin = adminService.getAdminInfo(Integer.parseInt(id));
		model.addAttribute("admin", userAdmin);
		model.addAttribute("page", "accountChange");
		if (update != null) {
			model.addAttribute("accountInfo", "change");
		} else {
			model.addAttribute("accountInfo", "onshow");
		}
		return "homepageFA";
	}

	@GetMapping("")
	public String homePage() {
		return "redirect:/admin/patientManage";
	}

	@PostMapping("activate")
	public String accountActivate(String idcard, RedirectAttributes attributes,
			@CookieValue(value = "id", required = false) String id) {
		String result = adminService.activateAccount(idcard, Integer.parseInt(id));
		if (result.equals("success")) {
			return "redirect:/admin";
		} else {
			attributes.addAttribute("activate", "activate");
			attributes.addAttribute("result", result);
			return "redirect:/admin/login";
		}

	}

	@GetMapping("login")
	public String login(Model model, @RequestParam(value = "result", required = false) String result,
			@RequestParam(value = "activate", required = false) String activate) {
		if (activate == null && result == null) {
			model.addAttribute("type", "login");
		} else if (activate == null && result != null) {
			model.addAttribute("type", "login");
			model.addAttribute("error", result);
		} else {
			model.addAttribute("type", "activate");
			model.addAttribute("error", result);
		}
		return "loginAdmin";
	}

	@PostMapping("login")
	public String loginByAdmin(UserAdmin userAdmin, Model model, RedirectAttributes attributes, HttpSession session) {
//		System.out.println(userAdmin);
		String result = adminService.loginByAdmin(userAdmin);
		if (result.endsWith("success")) {
			String arr[] = result.split("/");
			setCookie(arr[0], arr[1]);
			session.setAttribute("userSection", arr[2]);
			session.setAttribute("userName", arr[3]);
			return "redirect:/admin";
		} else if (result.endsWith("activate")) {
			String arr[] = result.split("/");
			setCookie(arr[0], arr[1]);
			session.setAttribute("userSection", arr[2]);
			session.setAttribute("userName", arr[3]);
			attributes.addAttribute("activate", "activate");
			return "redirect:/admin/login";
		} else {
			attributes.addAttribute("result", result);
			return "redirect:/admin/login";
		}
	}

}
